package cn.com.nes.common.util;

import cn.com.nes.site.entity.OtherData;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * gzip压缩工具类
 *
 * @author nes
 */
public class ZipUtils {

    /**
     * 使用gzip进行压缩
     */
    public static String gzip(String primStr) {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(primStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    }

    /**
     * <p>Description:使用gzip进行解压缩</p>
     *
     * @param compressedStr
     * @return
     */
    public static String gunzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString("utf-8");
        } catch (IOException e) {
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }

    /**
     * 处理other data
     */
    public static List<OtherData> processOtherData(String rawData) {
        List<OtherData> res = new ArrayList<>();
        if (StrUtil.isEmpty(rawData)) {
            return res;
        }
        //解压other data
        String otherDataJson = ZipUtils.gunzip(rawData);
        //原始数据如果为压缩,解压结果为空,则取原始数据
        if (StrUtil.isEmpty(otherDataJson)) {
            otherDataJson = rawData;
        }
        if (StrUtil.isNotEmpty(otherDataJson)) {
            res = JSONArray.parseArray(otherDataJson, OtherData.class);
        }
        return res;
    }

    public static Map<String, String> getOtherDataMap(String rawData) {
        Map<String, String> res = new HashMap<>();
        List<OtherData> otherDataList = ZipUtils.processOtherData(rawData);
        if (CollUtil.isNotEmpty(otherDataList)) {
            for (OtherData o :
                    otherDataList) {
                String f = o.getF();
                String s = o.getS();
                res.put(f, s);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        String otherData = "H4sIAAAAAAAAAIVZzY4cNRB+l7mSWXWV7Xabd+DGDXEAhVw5ROwFIe0egAiyyR5AILQcVkgQDtlE" +
"BJbVaqO8zM7M5sQrMNPp+lx2257TWCq3Xa6f76uq+ejLxYPF+4tPmPrFvcXD7XLY/h6Ov1/dm4R2"+
"mGR+kvko662fhEvXTeLdChu4p2kDiZyUmHo3iS1P4u0CYu/CJHb9JN4u4uHjnTtxL4f3yeEiZijH"+
"WjnycrwXuU/EDrpH5fXjrJUN4+pwWqkN8vrxnYfvFlHM8r0Jk3i7iPfD8kRind0qbjDyQCP6Ga0/"+
"jif5WgmH5t1M4lh5mH4W3GLlWVY/y/WwG/TulN5mEMO67kDOH5dK99ByLQcziYNoEJQGxonduwP5"+
"fPXocvXNv+tn56ubp+qeUcNx57Tvg8/vf/rFw7dHv9xdvFidvVz9eqStZs0el8DjlQ22Q9BZi8eP"+
"S+UbBJ7kIw1aLL5hEfOggw5fM8KWdVwM8ualBMZSvZE9NVwf5G4TDiQnx2U0fjBiJCPOMU6b3GUm"+
"3/x+vPnhmc4LuYQkNimJTTmgmFUOLhIp6dsdrCOBcXOkAG2glpgAlBB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OU"+
"J1WzcYLmsGjDSs5bycgSGoUczJIBGmigOKToMMCqtKDcJhmA2ZzbGP1Lhb5y5tRquzyoVk++39z8"+
"uT673rU4Z9c6dkAVpbqDORqA46BqXEfv9XkHlBMaoeBkBBhzUlAD9y18aFPURGUGW7vE1tQoVBhT"+
"DofjXXJ8t3cDWu4KrHOzVQNx1dRHLBXbGdS0RcJ3MxRJRuQNvqUILkW+7TD8r/oWnXixwRTHL+H4"+
"ZYIs1J4BI3XL9ThaxG0HIRvGZTwBvVKNPXycpYhfdeU9zHIpaVIx6anELfWzYkmPuBhNQ/F2Mvl0"+
"TsNHA7lcW23CXKIITBSLfshJb+C+2eRtCT0Pm6Q7RvXqYhOmUQfRXi3CCE1iscjZKtBoDRh/upRb"+
"VN/AZepm/sxaltC42QCZKf5pcXOUQvLQnHpimluZ/LlWi9rvG/zBr8VwZKABw3PsEtPlVWBSB+Rd"+
"0exPAYqVCiKjSxTAf0KluQwNrevjfyaVYRtq/0phTijMy5xsQ/sCgvmyDR//D8gnMx+lHQAA";
        System.out.println(ZipUtils.gunzip(otherData));
    }

    public static Map<String, String> getOtherDataMap(String rawData, List<String> fld) {
        Map<String, String> res = new HashMap<>();
        List<OtherData> otherDataList = ZipUtils.processOtherData(rawData);
        if (CollUtil.isNotEmpty(otherDataList)) {
            for (OtherData o :
                    otherDataList) {
                String f = o.getF();
                String s = o.getS();
                if (fld.contains(f)) {
                    res.put(f, s);
                }
            }
        }
        return res;
    }
}


